const db = require('../dal/mongo')
const ObjectId = require('mongodb').ObjectId
class ArticleController{
     static async getAllArticles (ctx,next) {
        const res = await db.find('article')
        ctx.success({
            msg: 'success',
            data: res
        })
    }
    static async getPublishedArticles (ctx,next) {
        const res = await db.find('article',{publish: true})
        ctx.success({
            msg:'success',
            data:res
        })
    }
    static async addOneArticle (ctx, next) {
        console.log(ctx.request)
        const res = await db.insertOne('article',ctx.request.body)
        ctx.success({
            msg:'success',
            data:res,
            success:true
        })
    }
    static async getOneArticleContent (ctx){
        let res = await db.find('article', {_id: ObjectId(ctx.query._id)})
        res = res[0].content
        ctx.success({
            msg:'success',
            data:res,
            success:true
        })
    }
    static async publishArticle (ctx){
        const param = ctx.request.body
        console.log(param)
        let res = await db.update('article', {_id: ObjectId(param._id)}, {publish: param.publish})
        ctx.success({
            msg:'success',
            data:res,
            success:true
        })
    }
    static async modifyArticle (ctx, next) {
        console.log(ctx.request)
        const res = await db.update('article',{_id:''}, ctx.request.body)
        ctx.success({
            msg:'success',
            data:res,
            success:true
        })
    }
}
module.exports = ArticleController

